import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Objects;

/**
 * 获取Gitee可用三位字母仓库名称
 * post请求，嫌慢可以开线程走异步（原理一样）
 */
public class GiteeName {
    public static void main(String[] args) {
        String url = "  https://gitee.com/check";
        System.out.println("开始查找");
        StringBuilder success = new StringBuilder();
        for (int i = 97; i <= 122; i++) {
            for (int j = 97; j <= 122; j++) {
                for (int k = 97; k <= 122; k++) {
                    String s = new String(new byte[]{(byte) i, (byte) j, (byte) k});
                    try {
                        String re = new String(post(url, "do=user_username&val=" + s));
                        if (re.equals("1")) {
                            System.out.println("成功:" + s);
                            success.append(s + "\n");
                        } else {
                            System.out.println(re + "：" + s);
                        }
                    } catch (Exception e) {
                        System.out.println("失败:" + s + " 原因：" + e.getMessage());
                    } finally {
                        //写入磁盘
                        if (s.equals("ZZZ")) byteToFile(success.toString().getBytes(), new File("D:/查询结果.txt"));
                    }
                }
            }
        }
    }

    public static byte[] post(String requestUrl, String requestBytes) throws Exception {
        return post(requestUrl, requestBytes.getBytes());
    }

    public static byte[] post(String requestUrl, byte[] requestBytes) throws Exception {
        HttpURLConnection urlConn = (HttpURLConnection) (new URL(requestUrl)).openConnection();
        urlConn.setConnectTimeout(5000);
        urlConn.setReadTimeout(5000);
        urlConn.setDoInput(true);
        urlConn.setUseCaches(false);
        urlConn.setRequestMethod("POST");
        urlConn.setInstanceFollowRedirects(true);
        urlConn.addRequestProperty("User-Agent", "PostmanRuntime/7.25.0");
        urlConn.addRequestProperty("Accept", "*/*");
        urlConn.addRequestProperty("Connection", "keep-alive");
        urlConn.addRequestProperty("Referer", "https://gitee.com/signup");
        urlConn.addRequestProperty("Host", "gitee.com");
        urlConn.addRequestProperty("Origin", "https://gitee.com");
        urlConn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        urlConn.setDoOutput(true);
        urlConn.setDoInput(true);
        urlConn.connect();
        DataOutputStream dos = new DataOutputStream(urlConn.getOutputStream());
        dos.write(requestBytes);
        dos.flush();
        dos.close();
        int responseCode = urlConn.getResponseCode();
        if (responseCode != 200) {
            throw new Exception("请求失败，错误码：" + responseCode);
        } else {
            byte[] bytes = inputStreamToBytes(urlConn.getInputStream());
            urlConn.disconnect();
            return bytes;
        }
    }

    public static byte[] inputStreamToBytes(InputStream inputStream) throws IOException {
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        while ((len = inputStream.read(buffer)) != -1) {
            bs.write(buffer, 0, len);
        }
        bs.flush();
        return bs.toByteArray();
    }

    public static boolean byteToFile(byte[] bytes, File file) {
        if (!(Objects.requireNonNull(file.getParentFile())).exists()) {
            file.getParentFile().mkdirs();
        }

        if (file.exists()) {
            file.delete();
        }

        try {
            FileOutputStream out = new FileOutputStream(file);
            out.write(bytes);
            out.flush();
            out.close();
            return true;
        } catch (FileNotFoundException var4) {
            System.out.println("No Find File");
            return false;
        } catch (IOException var5) {
            System.out.println("IO Error");
            return false;
        }
    }
}
